package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.AdminProfileService;
import com.cskaoyan.market.service.impl.AdminProfileServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author:yanzijun
 * @time:2024/06/20/11:09
 */
@WebServlet("/admin/profile/*")
public class AdminProfileController extends HttpServlet {
    AdminProfileService adminProfileService = new AdminProfileServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replaced = requestURI.replace(req.getContextPath() + "/admin/profile/", "");
        if ("password".equals(replaced)) {
            password(req, resp);
        }
    }

    private void password(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String readLine = req.getReader().readLine();
        String oldPassword = JacksonUtil.parseString(readLine, "oldPassword");
        String newPassword = JacksonUtil.parseString(readLine, "newPassword");
        String newPassword2 = JacksonUtil.parseString(readLine, "newPassword2");
        HttpSession session = req.getSession();
        MarketAdmin admin = (MarketAdmin) session.getAttribute("admin");
        if (admin == null) {
            Object o = ResponseUtil.fail();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
            return;
        }
        String username = admin.getUsername();
        int num = adminProfileService.password(username, oldPassword, newPassword, newPassword2);
        //num=0 修改成功
        //num=1 密码与原密码不相等
        //num=2 输入的密码为空
        //num=3 修改的密码与原密码一致
        //num=4 输入的密码格式不对
        //num=5 两次`密码输入不一致
        Object o = null;
        if (num == 0) {
            o = ResponseUtil.ok();
        } else if (num == 1) {
            o = ResponseUtil.fail(MarketConstant.WRONG_PASSWORD, "原密码错误");
        } else if (num == 2) {
            o = ResponseUtil.fail(MarketConstant.WORNG_PASSISEMPTY, "密码不能为空");
        } else if (num == 3) {
            o = ResponseUtil.fail(MarketConstant.WRONG_PASSWORDSAME, "不能与原密码相同");
        } else if (num == 4) {
            o = ResponseUtil.fail(MarketConstant.WRONG_PASSWORDFORMAT, "密码至少需要一个大写字母,一个小写字母,一个数字,且长度大于6");
        } else if (num == 5) {
            o = ResponseUtil.fail(MarketConstant.WRONG_PASSWORDNOTSAME, "两次密码一致");
        }
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().write(s);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replaced = requestURI.replace(req.getContextPath() + "/admin/profile/", "");
        if ("nnotice".equals(replaced)) {
            Object ok = ResponseUtil.ok("0");
            String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s);
        }
    }
}
